package com.meteosurfing;



import android.app.Activity;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class Configuracion extends Activity {
    public static final String PREFS_NAME = "MeteoWidgetPrefs";
    
    public static final String PREFS_TIEMPO_ENTRE_ALARMAS_FIELD_PATTERN = "TiempoEntreAlarmas";
    private static final int TIEMPO_ENTRE_ALARMAS = 20;
    public static final String PREFS_VIENTO_FIELD_PATTERN = "Viento";
    private static final int VIENTO = 12;
    public static final String PREFS_NUMERO_DE_ALARMAS_FIELD_PATTERN = "NumeroDeAlarmas";
    private static final int NUMERO_DE_ALARMAS = 2;
    public static final String PREFS_NUMERO_DE_ALARMAS_PENDIENTES_FIELD_PATTERN = "NumeroDeAlarmasPendientes";
    public static final String PREFS_ALRMA_ACTIVADA_FIELD_PATTERN = "AlarmaActivada";
    private static final boolean ALARMA_ACTIVADA = false;
    public static final String URI_SCHEME = "configuracion_widget";    

    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get any data we were launched with
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        if (extras != null) {
            appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

            Intent cancelResultValue = new Intent();
            cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_CANCELED, cancelResultValue);
        } else {
            // only launch if it's for configuration
            // Note: when you launch for debugging, this does prevent this
            // activity from running. We could also turn off the intent
            // filtering for main activity.
            // But, to debug this activity, we can also just comment the
            // following line out.
           // finish();
        }

        setContentView(R.layout.wconfiguracion);

        final SharedPreferences config = getSharedPreferences(PREFS_NAME, 4);
     
        final EditText tiempoEntreAlarmasEntry = (EditText) findViewById(R.id.tiempo_alarmas);
        final EditText numeroDeAlarmasEntry = (EditText) findViewById(R.id.numero_alarmas);
        final EditText vientoEntry = (EditText) findViewById(R.id.viento);
        final CheckBox alarmaActivadaEntry = (CheckBox) findViewById(R.id.activarAlarma);
        
        tiempoEntreAlarmasEntry.setText(String.valueOf(config.getInt(PREFS_TIEMPO_ENTRE_ALARMAS_FIELD_PATTERN, TIEMPO_ENTRE_ALARMAS)));
        numeroDeAlarmasEntry.setText(String.valueOf(config.getInt(PREFS_NUMERO_DE_ALARMAS_FIELD_PATTERN, NUMERO_DE_ALARMAS)));
        vientoEntry.setText(String.valueOf(config.getInt(PREFS_VIENTO_FIELD_PATTERN, VIENTO)));
        alarmaActivadaEntry.setChecked(config.getBoolean(PREFS_ALRMA_ACTIVADA_FIELD_PATTERN, ALARMA_ACTIVADA));
      
        Button saveButton = (Button) findViewById(R.id.save_button);

        
        
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        setResult(RESULT_OK, resultValue);
        

    	
        
        
        
        saveButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            	int tiempoEntreAlarmasSeconds = Integer.parseInt(tiempoEntreAlarmasEntry.getText().toString());
                int numeroDeAlarmasSeconds = Integer.parseInt(numeroDeAlarmasEntry.getText().toString());
                int viento = Integer.parseInt(vientoEntry.getText().toString());
                
                boolean alarmaActivada = alarmaActivadaEntry.isChecked();
                
                // store off the user setting for update timing
                SharedPreferences.Editor configEditor = config.edit();
//                configEditor.clear();
                
                configEditor.putInt(String.format(PREFS_TIEMPO_ENTRE_ALARMAS_FIELD_PATTERN, 1), tiempoEntreAlarmasSeconds);
                configEditor.putInt(String.format(PREFS_NUMERO_DE_ALARMAS_FIELD_PATTERN, 1), numeroDeAlarmasSeconds);
                configEditor.putInt(String.format(PREFS_NUMERO_DE_ALARMAS_PENDIENTES_FIELD_PATTERN, 1), numeroDeAlarmasSeconds);
                
                configEditor.putInt(String.format(PREFS_VIENTO_FIELD_PATTERN, 1), viento);
                configEditor.putBoolean(String.format(PREFS_ALRMA_ACTIVADA_FIELD_PATTERN,1),alarmaActivada);
                
                configEditor.commit();

              //  if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                	{
                    // tell the app widget manager that we're now configured
                    Intent resultValue = new Intent();
                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    setResult(RESULT_OK, resultValue);
                }

                finish();
            }
        });
    }
}